package cxy.highConcurrency;


/**
 * Thread.sleep(100);会抛出InterruptedException异常，而这个抛出这个异常会清除当前线程的中断标识，导致th.isInterrupted()一直都是返回false的。
 * InterruptedException - if any thread has interrupted the current thread. 
 * The interrupted status of the current thread is cleared when this exception is thrown.
 * @author admin
 *
 */
public class InterruptedExceptionDemo {

	public static void main(String[] args) {
		
		Thread t1 = new Thread(() -> {
			Thread th = Thread.currentThread();
			while (true) {
				if (th.isInterrupted()) {
					System.out.println("isInterrupted");
					break;
				}
				// 省略业务代码无数
				System.out.println("业务代码");
				try {
					Thread.sleep(100);
				//异常的触发条件就是：其他线程调用了该线程的 interrupt() 方法。
				} catch (InterruptedException e) { 
					System.out.println("捕获异常");
					//应该重置一下中断标示，因为抛出异常后，中断标示会自动清除掉！
					//Thread.currentThread().interrupt();
					e.printStackTrace();
				}
			}
			System.out.println("over");
		});
		
		t1.start();
		System.out.println("主线程");
		t1.interrupt();
		
	}
}
